﻿
Imports System.IO
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports storoomc

Public Class counter
    Inherits visitCounter

    Public Shadows Const _pathidentity As String = "/homeservecounter"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function read(ByVal key As String, ByRef count As Int64) As Boolean
        Dim buff As bytes = Nothing
        If storoom.read(key, buff) AndAlso convertor.toint64(encoding.utf8.GetString(buff.data), count) Then
            Return True
        Else
            raiseError("cannot read key " + key + " from storoom.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Protected Overrides Function write(ByVal key As String, ByVal count As Int64) As Boolean
        On Error Resume Next
        Dim buff As bytes = Nothing
        buff = New bytes(encoding.utf8.GetBytes(Convert.ToString(count)))
        If Not buff Is Nothing AndAlso storoom.modify(key, buff) Then
            Return True
        Else
            raiseError("cannot write key " + key + " back to storoom.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Protected Overrides Function processOutput(ByVal context As httpContext, ByVal count As Int64) As Boolean
        Dim buff As MemoryStream = Nothing
        buff = context.buff()

        Dim page As xhtmlpage = Nothing
        page = New xhtmlpage(buff)
        page.title().text = applicationSign() + " - count"
        page.addcsslink(csslinkhref)
        Dim label As label = Nothing
        label = New label(buff)
        label.text = "you are the " + Convert.ToString(count) + " visitors."
        page.body().addchild(label)

        context.setstatuscode(Net.HttpStatusCode.OK, "OK")
        context.setcontentType(network.http.contentType.texthtmlUtf8)
        context.setcontentEncoding(encoding.utf8)

        If page.render() Then
            Return True
        Else
            raiseError("cannot render xhtmlpage, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Protected Overrides Function processHomepage(ByVal context As httpContext) As Boolean
        Dim buff As MemoryStream = Nothing
        buff = context.buff()

        Dim page As xhtmlpage = Nothing
        page = New xhtmlpage(buff)
        page.title().text = applicationSign() + " - count error page"
        page.addcsslink(csslinkhref)
#If RETIRED Then
        page.body().text = "invalid key found for homeserve, key " + key
#End If
        page.body().text = "invalid key found for homeserve"

        context.setstatuscode(Net.HttpStatusCode.OK, "OK")
        context.setcontentType(network.http.contentType.texthtmlUtf8)
        context.setcontentEncoding(encoding.utf8)

        If page.render() Then
            Return True
        Else
            raiseError("cannot render xhtmlpage, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function
End Class
